/* Viper Split# Compiler
 * Copyright (c) 2013 Split# Developers
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 */


/* NOTE: The above license DOES NOT APPLY to the Viper and GruntXProductions
 * namespace. Viper is released under the GNU General Public License v3
 */

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using GruntXProductions;
using Viper;
using Viper.Opcodes;

namespace SplitSharp
{
        public partial class CodeGen
        {
                public void compileIf (IfStatement ifstat)
                {
                        VType type = getType (ifstat.Expression.Expression1.tokens);
                        if (type == VType.Dword || type == VType.Pointer) {
                                string v1 = declareTempDword ("if_l1");
                                string v2 = declareTempDword ("if_r1");
                                int i = ifIndex;
                                ifIndex++;
                                ParseArthemeticExpression (ifstat.Expression.Expression1.tokens);
                                Assembler.Emit (new push_ptr (v1));
                                Assembler.Emit (new dstore ());
                                ParseArthemeticExpression (ifstat.Expression.Expression2.tokens);
                                Assembler.Emit (new push_ptr (v2));
                                Assembler.Emit (new dstore ());
                                Assembler.Emit (new push_ptr (v1));
                                Assembler.Emit (new dload ());
                                Assembler.Emit (new push_ptr (v2));
                                Assembler.Emit (new dload ());
                                if (ifstat.Expression.Operation == BoolOp.Equal)
                                        Assembler.Emit (new bne ("endif" + i.ToString ()));
                                else if (ifstat.Expression.Operation == BoolOp.GreaterThan)
                                        Assembler.Emit (new blt ("endif" + i.ToString ()));
                                else if (ifstat.Expression.Operation == BoolOp.LessThan)
                                        Assembler.Emit (new bgt ("endif" + i.ToString ()));
                                CompileBlock (ifstat.ifBody);
                                Assembler.Emit (new bra ("else" + i.ToString ()));
                                Assembler.CreateLabel ("endif" + i.ToString ());
                                CompileBlock (ifstat.elseBody);
                                Assembler.CreateLabel ("else" + i.ToString ());

                        } else {
                                string v1 = declareTempDword ("if_l1");
                                string v2 = declareTempDword ("if_r1");
                                int i = ifIndex;
                                ifIndex++;
                                ParseStringExpression (ifstat.Expression.Expression1.tokens);
                                Assembler.Emit (new push_ptr (v1));
                                Assembler.Emit (new dstore ());
                                ParseStringExpression (ifstat.Expression.Expression2.tokens);
                                Assembler.Emit (new push_ptr (v2));
                                Assembler.Emit (new dstore ());
                                Assembler.Emit (new push_ptr (v1));
                                Assembler.Emit (new dload ());
                                Assembler.Emit (new push_ptr (v2));
                                Assembler.Emit (new dload ());
                                Assembler.Emit (new sysf (4));
                                if (ifstat.Expression.Operation == BoolOp.Equal)
                                        Assembler.Emit (new bz ("endif" + i.ToString ()));
                                else if (ifstat.Expression.Operation == BoolOp.NotEqual)
                                        Assembler.Emit (new bnz ("endif" + i.ToString ()));
                                else
                                        Errors.Add ("Invalid comparison between string and string!");
                                CompileBlock (ifstat.ifBody);
                                Assembler.Emit (new bra ("else" + i.ToString ()));
                                Assembler.CreateLabel ("endif" + i.ToString ());
                                CompileBlock (ifstat.elseBody);
                                Assembler.CreateLabel ("else" + i.ToString ());

                        }

                }
        }
}